import {createRouter, createWebHashHistory} from 'vue-router';
import store from "@/store";
import axios from "@/http";
import Home from "@/components/Home.vue";
import Login from "@/components/Login.vue";
import Index from "@/components/Index.vue";
import User from "@/views/system/User.vue";
import Role from "@/views/system/Role.vue";
import Menu from "@/views/system/Menu.vue";
import Profile from "@/views/system/Profile.vue";
import auth from "@/utils/auth";
import Test from "@/views/system/Test.vue";


const routes = [
    {
        path: '/index', component: Index, name: 'index',
        redirect: '/system/profile',
        children: [
            {path: '/system/users', component: User, name: 'users'},
            {path: '/system/roles', component: Role, name: 'roles'},
            {path: '/system/menus', component: Menu, name: 'menus'},
            {path: '/system/profile', component: Profile, name: 'profile'},
            {
                path: '/system/user_center',
                component: () => import("@/views/system/UserCenter.vue"),
                name: 'user_center'
            },
            {path: '/system/test', component: Test, name: 'test'},

        ]
    },
    {path: '/', component: Home, name: 'home'},
    {path: '/login', component: Login, name: 'login'},

]

const router = createRouter({
    history: createWebHashHistory(),
    mode: 'history',
    routes,
})


function menuToRoute(e) {
    if (e.component === 'Layout') return null
    let route = {path: '', name: '', component: null, meta: {}}
    route.name = e.name;
    route.path = e.path;
    // let c = (() => import("@/views/system/UserCenter.vue"))
    // console.log(User)
    // console.log(c)
    // route.component = () => import(`@/views/${e.component}.vue`);
    route.meta.icon = e.icon;
    route.meta.title = e.title;
    route.meta.index = e.index;

    return route
}

router.beforeEach((to, from, next) => {

    // getMenusList
    if (store.state.menuList === undefined) {
        axios.post(`/system/menus/list`).then(async resp => {
            // store.commit('setMenuList', resp.data.data)
            store.state.menuList = resp.data.data
        }).catch(err => {
            console.log(err)
        }).finally(async () => {
            // 动态绑定路由
            let routes = router.options.routes;
            if (store.state.menuList) {
                store.state.menuList.forEach(menus => {
                    if (menus.children) {
                        menus.children.forEach(e => {
                            // 转换成路由
                            let route = menuToRoute(e);
                            // 添加路由
                            if(route !== null){
                                routes[0]['children'].push(route);
                            }

                        })
                    }
                })
            }
            router.addRoute(routes)
        })
    }
    // getAuthorities
    if (store.state.authorities === undefined) {
        axios.post(`/system/menus/authorities`).then(resp => {
            // store.commit('setAuthorities', resp.data.data)
            store.state.authorities = resp.data.data;
        }).catch(err => {
            console.log(err)
        })
    }

    if(auth.token === null){
        if(to.path !== '/login'){
            router.replace('/login').then(err => {
                console.log(err)
            })
        }
    }

    next()
})


export default router;
